﻿using Alone.EnemyStates;
using AloneLibrary.GameObjects;
using AloneLibrary.Services.Camera;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using XNAnimation;
using XNAnimation.Controllers;
using XNAnimation.Effects;

namespace Alone.GameObjects.Enemy
{
	public class Enemy : DynamicGameObject
	{
		private EnemyState currentState;
		private bool isAlive;
		private Matrix turnFactor;

        /// <summary>
        /// Gets or sets a value indicating whether this instance is alive.
        /// </summary>
        /// <value><c>true</c> if this instance is alive; otherwise, <c>false</c>.</value>
		public bool IsAlive
		{
			get { return isAlive; }
			set { isAlive = value; }
		}

        /// <summary>
        /// Gets or sets the state of the current.
        /// </summary>
        /// <value>The state of the current.</value>
		public EnemyState CurrentState
		{
			get { return currentState; }
			set { currentState = value; }
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="Enemy"/> class.
		/// </summary>
		/// <param name="model">The model.</param>
		/// <param name="position">The position.</param>
		/// <param name="direction">The direction.</param>
		/// <param name="scale">The scale.</param>
		/// <param name="velocity">The velocity.</param>
		public Enemy(SkinnedModel model, AnimationController animationController, Effect effect, Vector3 position, Vector2 rotation, float scale, Vector3 velocity)
			: base(model, animationController, effect, position, rotation, scale, velocity)
		{
			this.ForwardSpeed = 1f;
			this.currentState = new StrayState();
			this.isAlive = true;
		}

		/// <summary>
		/// Draws the specified game time.
		/// </summary>
		/// <param name="gameTime">The game time.</param>
		/// <param name="camera">The camera.</param>
		public override void Draw(GameTime gameTime, ICameraHandler camera)
		{

			Matrix[] transforms = new Matrix[skinnedModel.Model.Bones.Count];
			skinnedModel.Model.CopyAbsoluteBoneTransformsTo(transforms);

			world = Matrix.CreateScale(Scale) *
			   Matrix.CreateRotationY(Rotation.Y) *
			   Matrix.CreateTranslation(Position);

			foreach (ModelMesh modelMesh in skinnedModel.Model.Meshes)
			{
				foreach (ModelMeshPart meshPart in modelMesh.MeshParts)
				{
					SkinnedModelBasicEffect basicEffect = (SkinnedModelBasicEffect)meshPart.Effect;

					basicEffect.World = world;
					basicEffect.Bones = this.animationController.SkinnedBoneTransforms;
					basicEffect.View = camera.View;
					basicEffect.Projection = camera.Projection;

					// OPTIONAL - Configure material
					basicEffect.Material.DiffuseColor = new Vector3(0.8f);
					basicEffect.Material.SpecularColor = new Vector3(0.3f);
					basicEffect.Material.SpecularPower = 8;

					basicEffect.NormalMapEnabled = false;
					basicEffect.SpecularMapEnabled = false;

					// OPTIONAL - Configure lights
					basicEffect.LightEnabled = true;
					basicEffect.AmbientLightColor = new Vector3(0.2f);
					basicEffect.EnabledLights = EnabledLights.Two;
					basicEffect.PointLights[0].Color = Vector3.One;
					basicEffect.PointLights[0].Position = new Vector3(100, 100, 100);
					basicEffect.PointLights[1].Color = Vector3.One;
					basicEffect.PointLights[1].Position = new Vector3(100, 100, -100);
				}
				modelMesh.Draw();
			}
		}

        /// <summary>
        /// Does the valid turn.
        /// </summary>
        /// <param name="obj">The obj.</param>
        public void DoValidTurn(StaticGameObject obj) {
            bool[] dirs = GetCollisionDirections(obj);

            turnFactor = Matrix.CreateRotationY(MathHelper.ToRadians(0));

            for (int i = 0; i < 4; i++) {
                if (dirs[i]) {
                    switch (i) {
                        case 1:
                            turnFactor = Matrix.CreateRotationY(MathHelper.Pi);
                            break;
                        case 2:
                            turnFactor = Matrix.CreateRotationY(MathHelper.PiOver2);
                            break;
                        case 3:
                            turnFactor = Matrix.CreateRotationY(-MathHelper.PiOver2);
                            break;
                    }
                }
            }
        }
	}
}
